package com.zagj.videocomparess.utils;

import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Chen qin lang on 2018/4/16.
 */

public class PreviewCameara {

    private final Config config;
    private final CameraSizeComparator cameraSizeComparator;
    private Camera.Size propPreviewSize;
    private Camera.Size propPictureSize;
    private Point mPicSize;
    private Point mPreSize;
    private Camera camera;

    public PreviewCameara() {
        config = new Config();
        config.minPreviewWidth = 720;
        config.minPictureWidth = 720;
        config.rate = 1.778f;
        cameraSizeComparator = new CameraSizeComparator();

    }

    public boolean open(int cameraId) {
        camera = Camera.open(cameraId);
        if (null != camera) {
            Camera.Parameters parameters = camera.getParameters();
            propPreviewSize = getPropPreviewSize(parameters.getSupportedPreviewSizes(), config.minPictureWidth, config.rate);
            propPictureSize = getPropPictureSize(parameters.getSupportedPictureSizes(), config.minPictureWidth, config.rate);
            parameters.setPreviewSize(propPreviewSize.width,propPreviewSize.height);
            parameters.setPictureSize(propPictureSize.width,propPictureSize.height);
            camera.setParameters(parameters);
            Camera.Size previewSize = parameters.getPreviewSize();
            Camera.Size pictureSize = parameters.getPictureSize();
            mPicSize =new Point(pictureSize.height,pictureSize.width);
            mPreSize =new Point(previewSize.height,previewSize.width);
            return true;
        }
        return false;
    }
    public void setPreviewTexture(SurfaceTexture previewTexture){
        if (null!=camera){
            try {
                camera.setPreviewTexture(previewTexture);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public boolean close() {
        if(camera!=null){
            try{
                camera.stopPreview();
                camera.release();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }
    public boolean preview() {
        if(camera!=null){
            camera.startPreview();
        }
        return false;
    }

    private Camera.Size getPropPictureSize(List<Camera.Size> supportedPictureSizes, int minPictureWidth, float rate) {
        Collections.sort(supportedPictureSizes, cameraSizeComparator);
        int i = 0;
        for (Camera.Size size : supportedPictureSizes) {
            if (size.height >= minPictureWidth && equalRate(size, rate)) {
                break;
            }
            i++;
        }
        if (i==supportedPictureSizes.size()){
            i=0;
        }
        return supportedPictureSizes.get(i);
    }

    private Camera.Size getPropPreviewSize(List<Camera.Size> supportedPictureSizes, int minPictureWidth, float rate) {
        Collections.sort(supportedPictureSizes, cameraSizeComparator);
        int i = 0;
        for (Camera.Size size : supportedPictureSizes) {
            if (size.height >= minPictureWidth && equalRate(size, rate)) {
                break;
            }
            i++;
        }
        if (i==supportedPictureSizes.size()){
            i=0;
        }
        return supportedPictureSizes.get(i);
    }

    private boolean equalRate(Camera.Size size, float rate) {
        float reaRate = (float) size.width / (float) size.height;
        if (Math.abs(reaRate - rate) <= 0.03) {
            return true;
        }else{
            return false;
        }

    }

    public Point getProppreSIze() {
        return mPreSize;
    }

    class Config {
        float rate; //宽高比
        int minPreviewWidth;
        int minPictureWidth;
    }

    private class CameraSizeComparator implements Comparator<Camera.Size> {
        public int compare(Camera.Size lhs, Camera.Size rhs) {
            // TODO Auto-generated method stub
            if (lhs.height == rhs.height) {
                return 0;
            } else if (lhs.height > rhs.height) {
                return 1;
            } else {
                return -1;
            }
        }

    }
}
